package edu.must.uh.servlet;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import edu.must.uh.impl.RegCodeImpl;
import edu.must.uh.util.R;
import edu.must.uh.util.ServletUtils;
import edu.must.uh.util.ToolsOfString;

@WebServlet("/RegCodeServlet")
public class RegCodeServlet extends BaseServlet {
    private static final long serialVersionUID = 12197442526341123L;
    private static Logger logger = Logger.getLogger(RegCodeServlet.class);  
    
    
    
    public Object getList(HttpServletRequest request, HttpServletResponse response) throws Exception {      
     
        RegCodeImpl regCodeImpl = new RegCodeImpl();
        Map<String, Object> searchcondition = ServletUtils.getsearchcondition(request);
        List<Map<String,Object>>resList = regCodeImpl.getList(searchcondition, " ORDER BY CREATETIME DESC");
        if(resList !=null && !resList.isEmpty()) {
            Integer indexInteger = resList.size();
            for (Map<String, Object> map : resList) {
                map.put("XUHAO", indexInteger--); 
                Object createTime = map.get("CREATETIME");
                if(createTime !=null) {
                    String result2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(createTime);
                   map.put("S_CREATETIME", result2); 
                }else {
                    map.put("S_CREATETIME", ""); 
                }
                
            }
        }
           
        return R.succ(resList);         
    }
    
    
    
    
    public Object delete(HttpServletRequest request, HttpServletResponse response) throws Exception {      
        
        RegCodeImpl regCodeImpl = new RegCodeImpl();
        String keyValue =ToolsOfString.NulltoEmpty(request.getParameter("ID")); 
        Integer resInteger = regCodeImpl.delete(keyValue);
        return R.succ(resInteger);         
    }
    
  public Object save(HttpServletRequest request, HttpServletResponse response) throws Exception {  
      
        RegCodeImpl regCodeImpl = new RegCodeImpl();
        String CODE =ToolsOfString.NulltoEmpty(request.getParameter("CODE"));  
        String GROUP_CODE_ID =ToolsOfString.NulltoEmpty(request.getParameter("GROUP_CODE_ID")); 
        
        List<Map<String,Object>> resList = regCodeImpl.getList(null, " AND CODE = '"+CODE+"'  AND GROUP_CODE_ID = '"+GROUP_CODE_ID+"'  ");
        if(resList != null && resList.size() > 0) {
            return R.fail("重複掃描：" + CODE); 
        }else {
            String sql = "INSERT INTO reg_label(GROUP_CODE_ID,CODE,CREATETIME)VALUES('"+GROUP_CODE_ID+"','"+CODE+"',now())";
            Integer resInteger = regCodeImpl.save(sql);
            return R.succ(resInteger); 
        }  
    }
    
    
    
   
}
